<?php

class Deadhead_info extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Deadhead_info':
	 * @var integer $id
	 * @var integer $fk_appointment_id
	 * @var string $Origin
	 * @var string $destination
	 * @var double $mileage
	 * @var string $fuel_name
	 * @var double $gas_fee
	 * @var double $bridge_charge
	 * @var string $deadhead_reason
	 * @var integer $state
	 * @var string $create_time
	 */
	//private $_fuel_name=array('plain_empty'=>'平原车头空驶油耗','ghat_empty'=>'山路车头空驶油耗','plain_min'=>'平原空驶或载货10吨以下油耗',
	//'ghat_min'=>'山路空驶或载货10吨以下油耗','plain_mid'=>'平原轻驶油耗','ghat_mid'=>'山路轻驶油耗','plain_max'=>'平原重驶油耗','ghat_max'=>'山路重驶油耗');
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'deadhead_info';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('driving_circuit', 'numerical', 'integerOnly'=>true),
			array('origin','length','max'=>50),
			array('destination','length','max'=>50),
			array('fuel_name','length','max'=>20),
			array('deadhead_reason','length','max'=>200),
			array('mileage, gas_fee, bridge_charge', 'numerical'),
			array('origin,destination,mileage,deadhead_reason,fuel_name', 'required'),
			array('mileage', 'match','pattern' => '/^\d{1,8}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多8位，小数位2位'),
			array('gas_fee, bridge_charge', 'match','pattern' => '/^\d{1,16}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多16位，小数位2位'),
		);
	}
	
	/**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate($on)
	{
		if($this->isNewRecord){
			$this->state=0;
			$this->create_time=date('Y-m-d G:i:s');
		}
		return true;
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}
	
	public function getFuelNames(){
		return Yii::app()->params['fuel_consumption'];
	}
	
	public function getFuelName($n=null){
		return Yii::app()->params['fuel_consumption'][$n==null?$this->fuel_name:$n];
	}
	
	public function getDrivingCircuitNames(){
		return Yii::app()->params['driving_circuit'];
	} 
	
	public function getDrivingCircuitName($n=null){
		return  Yii::app()->params['driving_circuit'][$n==null?$this->driving_circuit:$n];
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '索引号',
			'fk_appointment_id' => '派车单id',
			'origin' => '始发地',
			'destination' => '目的地',
			'mileage' => '行驶里程',
			'driving_circuit' => '行驶线路', //0高速 1国道 2辅路
			'fuel_name' => '标准油耗',//对应标准油耗表内8种状况之一 plain_empty、ghat_empty、plain_min.....
			'gas_fee' => '油费',
			'bridge_charge' => '路桥费',
			'deadhead_reason' => '空驶原因',
			'state' => '状态',
			'create_time' => '创建时间',
		);
	}
}